<?php

// 公共助手函数

use app\common\library\Auth;
use think\Cache;

/**
 * 获取中英文数据
 */
if (!function_exists('ze')) {
    function ze($date, $name)
    {
        $language = (int) cookie('language') ? (int) cookie('language') : 2;
        if ($language == 1) {
            return $date['e_' . $name];
        } else {
            return $date[$name];
        }
    }
}


/**
 * 获取配置项数据
 * @param $name 名称
 * @param $stu 获取字段 title 和 value
 * @param string $m 如果stu为value返回的数据序号
 */
if (!function_exists('__config')) {
    function __config($name, $stu = 'title', $m = 0)
    {
        $language_stu = (int) cookie('language') ? (int) cookie('language') : 2;//1=English,2=Chinese
        $name .= ($language_stu == 1) ? '_en' : '';

        //生成配置文件
        $official_config_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_config.php';
        if (!file_exists($official_config_url)) {
            $config_all = [];
            $data = db('official_config')->select();
            foreach ($data as $item) {
                $config_all[$item['name']] = $item;
            }
            file_put_contents(
                $official_config_url,
                '<?php' . "\n\nreturn " . var_export_short($config_all) . ";\n"
            );
        } else {
            $config_all = include $official_config_url;
        }

        $config = $config_all[$name] ?? '';
        if (empty($config)) return '';
        if ($stu == 'title') return $config[$stu];
        if ($stu == 'value') {
            $array = explode('|', $config['value']);
            if ($config['type'] == 'image') {
                return cdnurl($array[$m]);
            }
            return $array[$m] ?? '';
        }
    }
}


/**
 * 获取中英文数据
 * @param $model_name  模型名称
 * @param $name 字段名称
 * @param string $stu 语言字段
 * @param string $m 模块
 * @return mixed|string
 * @throws \think\db\exception\DataNotFoundException
 * @throws \think\db\exception\ModelNotFoundException
 * @throws \think\exception\DbException
 */
if (!function_exists('__l')) {
    function __l($model_name, $name, $m = 'official')
    {
        $auth = Auth::instance();
        $language_stu = (int) cookie('language') ? (int) cookie('language') : 2;//1=English,2=Chinese
        $language = ($language_stu == 1) ? 'en' : 'zh-cn';
        $url = ROOT_PATH . "addons/$m/lang/$language/official_$model_name.php";
        if (file_exists($url)) {
            $data = include $url;
            if (isset($data[$name]) && !empty($data[$name])) {
                return $data[$name];
            }
        }

        $common = ROOT_PATH . "addons/$m/lang/$language.php";
        if (file_exists($common)) {
            $common_f = include $common;
            if (isset($common_f[$name]) && !empty($common_f[$name])) {
                return $common_f[$name];
            }
        }
        return $name;
    }
}


/**
 * 获取自己地址栏的数据
 */
if (!function_exists('get_pid_sid')) {
    function get_pid_sid($data = [])
    {
        $array = $_GET;
        if (count($data)) {
            foreach ($data as $key => $item) {
                $array[$key] = $item;
            }
        }
        $ur = '';
        if (count($array)) {
            foreach ($array as $key => $item) {
                if ($item) {
                    if ($ur) {
                        $ur .= "&$key=" . $item;
                    } else {
                        $ur .= "?$key=" . $item;
                    }
                }
            }
        }
        return $ur;
    }
}

/**
 * 栏目跳转
 */
if (!function_exists('get_cate_url')) {
    function get_cate_url($category)
    {
        //判断是不是外链
        if ($category['status'] == 3) {
            return $category['url'];
        }

        //判断是不是一级栏目
        $pid = $category['id'];
        $sid = $category['id'];
        $get_data = [':pid'=>$pid,':sid'=>$sid];

        if ($category['pid'] != 0) {
            //如果是有父级的
            $official_tree_category_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_tree_category.php';
            if (file_exists($official_tree_category_url)) {
                $tree_category_all = include $official_tree_category_url;
                foreach ($tree_category_all as $item) {
                    if ($item['id'] == $category['pid']) {
                        $father_category = $item;
                    }
                }
            }
            if (isset($father_category) && !empty($father_category)) {
                $pid = $father_category['id'];
                $get_data = [':pid'=>$pid,':sid'=>$sid];
            }
        }
        //判断是不是单页
        if ($category['status'] == 1) {
            return addon_url('official/web.official_home/single',$get_data,true,true);
        }
        //判断是不是列表
        if ($category['status'] == 2) {
            //生成配置文件
            $official_rule_url = ADDON_PATH . 'official' . DS . 'controller' . DS . 'common' . DS . 'official_rule.php';
            if (!file_exists($official_rule_url)) {
                $official_rule = [];
                $rule_all = db('official_rule')->select();
                foreach ($rule_all as $item) {
                    $official_rule[$item['id']] = $item;
                }
                file_put_contents(
                    $official_rule_url,
                    '<?php' . "\n\nreturn " . var_export_short($official_rule) . ";\n"
                );
            } else {
                $official_rule = include $official_rule_url;
            };
            $official_rule = $official_rule[$category['official_rule_id']] ?? [];
            return addon_url('/official/web.official_' . str_replace('official_list_', '', $official_rule['table_name'] ?? '') . '/list',$get_data,true,true);
        }
    }
}


